package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;


import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.SiteDao;
import com.hrbxlh.util.pageUtil;
/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：SiteActionImpl.java
* Description：上课地点actionImpl类
* History：
* Date           Author               Desc
* 2017.04.11	  xzx                  建立	
* -------------------------------------------------------
*/
@Component("siteActionImpl")
@Transactional
public class SiteActionImpl {

	@Resource(name = "siteDao")
	private SiteDao siteDao;
	
	@Resource(name = "departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;

	org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SiteActionImpl.class.getName());
	
	/**
	 * @description 添加地点
	 * @param Map<String,Object> paramMap  地点名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> addSite(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
//			//1013 输入过滤
//			paramMap.put("siteName", ParamConvertUtil.replaceHTMLCodeOne((String)paramMap.get("siteName")));
			List<Map<String, Object>> siteList = siteDao.getSiteByName(paramMap);
			//判断地点名重复
			if(siteList != null && siteList.size() != 0){
				failMsg = "该地点名已存在";
			}else{
				//添加地点
				siteDao.addSite(paramMap);
				result = true;
			}
			if(!result){
				resultMap.put("failMsg", failMsg);
			}
			resultMap.put("result", result);
			logger.info("添加地点，参数为："+paramMap.toString());
		} catch (Exception e) {
			logger.error("添加地点异常addSite", e);
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 修改地点
	 * @param  paramMap  地点名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveSite(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
//			//1013过滤输入
//			paramMap.put("siteName", ParamConvertUtil.replaceHTMLCodeOne((String)paramMap.get("siteName")));
			List<Map<String, Object>> siteList = siteDao.getSiteByName(paramMap);
			//判断地点名重复
			if(siteList != null && siteList.size() != 0){
				failMsg = "该地点名已存在";
			}else{
				//修改地点
				siteDao.saveSite(paramMap);
				result = true;
			}
			if(!result){
				resultMap.put("failMsg", failMsg);
			}
			resultMap.put("result", result);
		} catch (Exception e) {
			logger.error("修改地点异常saveSite", e);
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 删除地点
	 * @param Map<String,Object> paramMap
	 * @return Map result存成功失败标识   failMsg存错误信息
	 * @throws Exception
	 */
	public Map<String, Object> delSites(List<String> list) throws Exception{
		
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			String[] tables = {"relation","relsiteprofession"};	
			String[] errMsg = {"课表","专业"};
			Map<String, Object> mapUse ;
			List<Boolean> listMark = new ArrayList<Boolean>();
			List<String> noIds = new ArrayList<String>();
			for(String siteId : list){
				mapUse = departMentActionImpl.getDepartmentDelMark("地点",tables, errMsg, "siteId", Integer.parseInt(siteId));
				if((Boolean)mapUse.get("result")){
					listMark.add(true);
				}else{
					//存放不能删除的地点的id
					noIds.add(siteId);
				}
			}
			if(list.size() == listMark.size()){
				//执行批量删除
				siteDao.delSites(list);
				result.put("result", true);
			}else{
				result.put("result", false);
				result.put("failMsg", departMentActionImpl.getErrorMsg(noIds, 2,"siteName", "地点"));
			}
			logger.info("删除地点，参数为："+list.toString());
		}catch(Exception e){
			e.printStackTrace();
			logger.error("删除地点异常delSites", e);
			result.put("result", false);
			result.put("failMsg", "删除失败");
		}
		return result;
	}
	
	/**
	 * @description 查询全部地点
	 * @param Map<String,Object> paramMap  
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSites() throws Exception {

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			resultList = siteDao.getSites();
		} catch (Exception e) {
			logger.error("查询全部地点异常getSites", e);
			throw e;
		}
		return resultList;
	}
	
	/**
	 * @description 添加专业与教学地点的关联信息
	 * @param Map<String,Object> paramMap  地点名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public boolean addSiteReProfession(Map<String, Object> paramMap) throws Exception {

		boolean result = false;
		try {
			//删除这个专业的教师的关联信息
			siteDao.delSiteReProfession(paramMap);
			//添加关联信息
			siteDao.addSiteReProfession(paramMap);
			result = true;
		} catch (Exception e) {
			logger.error("添加专业与教学地点的关联信息异常addSiteReProfession", e);
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 获取某个专业与某个地点是不是关联信息（也可查询某个专业的教师地点信息）
	 * @param  专业id  地点id
	 * @return List
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSiteReProfession(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = siteDao.getSiteReProfession(paramMap);
		} catch (Exception e) {
			logger.error("获取某个专业与某个地点是不是关联信息异常getSiteReProfession", e);
			throw e;
		}
		return list;
	}
	
	/**
	 * @description 地点管理列表  分页
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getSiteList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			int pageNow = Integer.parseInt(paramMap.get("pageNow").toString());
			int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> siteList = siteDao.getSiteList(paramMap);
			resultMap.put("siteList", siteList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = siteDao.getSiteList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		}catch(Exception e){
			logger.error("地点管理异常getSiteList", e);
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询某地点的详细信息  
	 * @param 地点id
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getSiteDetail(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			resultMap = siteDao.getSiteDetail(paramMap);
		}catch(Exception e){
			logger.error("查询某地点的详细信息异常getSiteDetail", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 获取图标信息
	 * @param 地点id
	 * @return List
	 * @throws Exception
	 */
	public Map<String,Object> getSchIconInfo() throws Exception{

		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			resultMap = siteDao.getSchoolIconDetail();
		}catch(Exception e){
			logger.error("获取图标信息异常getSchIconInfo", e);
			throw e;
		}
		return resultMap;
	}
	/**
	 * @description 上传or修改图标
	 * @param paramMap Map<String, Object> 图标名称
	 * @return Map<String, Object>
	 * @throws Exception
	 */
	public Map<String,Object> addSchoolIcon(Map<String, Object> paramMap) throws Exception {

		boolean flag = false;
		Map<String,Object> returnMap = new HashMap<String, Object>();
		try {
			if(Integer.parseInt(String.valueOf(paramMap.get("schIconId")))!=0){
				//修改
				flag = siteDao.updateSchoolIcon(paramMap);
			}else{
				flag = siteDao.addSchoolIcon(paramMap);
			}
			//查询图标
			Map<String,Object> iconMap = siteDao.getSchoolIconDetail();
			returnMap.put("flag",flag);
			returnMap.put("schoolIcon",iconMap.get("schoolIcon"));
			returnMap.put("updateTime",iconMap.get("updateTime"));
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}

	/**
	 * @description 开启or禁用
	 * @param  paramMap  mark 0禁用 1开启
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveSchMark(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		try {
			result = siteDao.saveSchMark(paramMap);
			if(!result){
				resultMap.put("failMsg", "操作失败");
				resultMap.put("result", result);
				return resultMap;
			}
			resultMap.put("failMsg", "");
			resultMap.put("result", result);
		} catch (Exception e) {
			logger.error("开启or禁用异常saveSchMark", e);
			throw e;
		}
		return resultMap;
	}

}
